<style lang="scss" scoped>
main {
  padding: 0 0 0.7rem;
  .apple_id {
    padding: 0.5rem 0.2rem 0;
  }
  ul {
    padding: 0 0.2rem 0 0.2rem;
    .code {
      padding: 0.2rem 0 0.2rem 0.3rem;
      background: #fff;
      z-index: 2;
      right: 0;
      bottom: -0.2rem;
      & > div {
        height: 0.3rem;
        padding: 0 0 0 0.2rem;
        & > div {
          border: 1px solid #e12825;
          padding: 0.05rem 0.16rem;
          position: relative;
          top: 0.02rem;
          &.active {
            background: #e1e1e1;
            color: #fff;
            border: none;
          }
        }
      }
    }
    li {
      height: 0.45rem;
      padding: 0.58rem 0 0.08rem;
      display: flex;
      // justify-content: center;
      align-items: flex-end;
      div {
        padding-right: 0.08rem;
      }
    }
  }
}
footer {
  padding-top: 0.9rem;
}
.border-l:after {
  content: " ";
  position: absolute;
  left: -1px;
  top: 0;
  width: 1px;
  height: 100%;
  border-left: 1px solid #e12825;
  color: #e12825;
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
  -webkit-transform: scaleX(0.5);
  transform: scaleX(0.5);
}
</style>

<template>
    <div class="set_pwd">
        <main class="box1 bgfff">
            <div class="apple_id">
                已绑定手机
                <span class="cred margin-l5">{{phone | toMobile}}</span>
            </div>
            <ul>
                <!-- <li class=" flex-col box-sizing border-b">
          <input type="tel" v-model="phone" class="box1" placeholder="请输入手机号">
        </li> -->
                <li class=" flex-col box-sizing border-b">
                    <input type="number" v-model="code" class="box1" placeholder="请输入短信验证码">
                    <div class="position-a code">
                        <div class="border-l">
                            <div class="box-center cred bor-r" :class="{active:hasCodeText}" @click="getCode">{{codeText}}</div>
                        </div>
                    </div>
                </li>
                <li class=" flex-col box-sizing border-b">
                    <input type="password" v-model="password" class="box1" placeholder="请输入新密码">
                </li>
                <li class=" flex-col box-sizing border-b">
                    <input type="password" v-model="copyPassword" class="box1" placeholder="请再次输入密码">
                </li>
            </ul>
        </main>
        <footer class="box-center footer font-14 box-center">
            <div class="cfff box-center" @click="setPwd">确认修改</div>
        </footer>
    </div>
</template>

<script>
export default {
  name: "set_pwd",
  data() {
    return {
      codeText: "获取",
      hasCodeText: false, // 获取验证码背景色
      phone: "",
      code: "",
      password: "",
      copyPassword: ""
    };
  },
  mounted() {
    this.phone = this.getCache("userInfo").mobile;
  },
  methods: {
    // 获取验证码
    getCode() {
      let that = this;
      let phone = this.phone;
      // 验证手机格式
      if (!this.hasPhone()) return;

      this.http
        .get("user/getCode", {
          phone: phone
        })
        .then(res => {
          console.log("请求成功11111111111" + JSON.stringify(res));
          let time = 60,
            $time;
          $time = setInterval(function() {
            that.hasCodeText = true;
            --time;
            that.codeText = time + "s后重新获取";
            if (time <= 0) {
              that.hasCodeText = false;
              that.codeText = "获取";
              clearInterval($time);
              console.log("清除定时器");
            }
          }, 1000);
        })
        .catch(res => {
          console.log("请求失败22222222222" + JSON.stringify(res));
        });
    },

    // 手机验证
    hasPhone() {
      let phone = this.phone;
      if (phone === "") {
        this.layer("请输入手机号");
        return false;
      }
      if (!this.hasMobile(phone)) {
        this.layer("请输入正确格式的手机号");
        return false;
      }
      return true;
    },

    //修改密码
    setPwd() {
      let that = this;
      let password = this.password,
        copyPassword = this.copyPassword,
        mobile = this.phone,
        code = this.code;

      if (!this.hasPhone()) return;

      if (code === "") {
        this.layer("请输入验证码");
        return;
      }
      if (this.password.length < 6) {
        this.layer("密码长度不能小于6位");
        return;
      }
      if (this.copyPassword !== this.password) {
        this.layer("两次密码不一样");
        return;
      }

      this.http
        .post("user/userUpdatePassWord", {
          password: password,
          code: code
        })
        .then(res => {
          that.layer("修改密码成功");
          setTimeout(() => {
            that.toRouter("login");
          }, 1200);
        })
        .catch(res => {});
    }
  }
};
</script>


